package com.software.levelupandroid.statistics;

import android.content.Context;

import androidx.annotation.NonNull;

import com.software.levelupandroid.entity.Result;
import com.software.levelupandroid.entity.Task;
import com.software.levelupandroid.home.CallBack;
import com.software.levelupandroid.home.ui.taskByPriority.TaskByPriorityRepository;
import com.software.levelupandroid.utils.JsonUtil;
import com.software.levelupandroid.utils.OkhttpUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
public class TaskByMonthRepository {
    private static TaskByMonthRepository taskByMonthRepository;
    private List<Task> taskList = new ArrayList<>();
    public static TaskByMonthRepository getTaskByMonthRepository(){
        if (taskByMonthRepository == null){
            taskByMonthRepository = new TaskByMonthRepository();
        }
        return taskByMonthRepository;
    }
    //根据月份获取task
    public void getTaskByMonth(CallBack<List<Task>> callBack, Context context, String month) {
        //发送HTTP GET请求以获取指定月份的任务数据
        Call call = OkhttpUtil.getRequest("task/getTasksByMonthAndStatus?month=" + month, context);
        //异步处理HTTP请求
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                //请求失败时调用回调的onFailure方法
                callBack.onFailure(e.getMessage());
            }
            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                //请求成功时处理响应
                String resp = response.body().string();
                Result<List<Task>> result = JsonUtil.convertJsonToTaskList(resp);
                System.out.println(result);
                if (result != null) {
                    if (result.getCode() != null && result.getCode().equals("200")) {
                        //请求成功且返回码为200时，调用回调的onSuccess方法
                        List<Task> tasks = result.getData();
                        System.out.println(tasks);
                        callBack.onSuccess(tasks);
                    } else {
                        //返回码不为200时，调用回调的onFailure方法
                        callBack.onFailure("result.getCode==null||result.getCode.isNotEquals('200'): 获取任务失败");
                    }
                } else {
                    //返回结果为空时，调用回调的onFailure方法
                    callBack.onFailure("result==null: 未成功获取数据");
                }
            }
        });
    }
}
